package org.example.kafka.selfserial;

import org.apache.kafka.clients.consumer.Consumer;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.example.kafka.common.CommonConstant;
import org.example.kafka.common.KafkaConstants;
import org.example.vo.DemoUser;

import java.util.Collections;

public class SelfSerialConsumer {
    private static KafkaConsumer<String, DemoUser> consumer = null;

    public static void main(String[] args) {
        //消息的消费者
        consumer = new KafkaConsumer<String, DemoUser>(KafkaConstants.consumerConfig("selfserial",
                StringDeserializer.class,
                SelfDeserializer.class));
        try{
            // 订阅主题
            consumer.subscribe(Collections.singletonList(CommonConstant.SELF_SERIAL_TOPIC));
            while (true){
                ConsumerRecords<String,DemoUser> records = consumer.poll(500);
                for(ConsumerRecord<String, DemoUser> record:records){
                    System.out.println(String.format(
                            "主题：%s，分区：%d，偏移量：%d，key：%s，value：%s",
                            record.topic(),record.partition(),record.offset(),
                            record.key(),record.value()));
                    //do our work
                }
            }
        }finally {
            consumer.close();
        }

    }

}
